package cn.sam416.sample.easymock.method;

import static org.easymock.EasyMock.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.ArrayList;

import org.junit.jupiter.api.Test;
//From: https://www.journaldev.com/22193/easymock-tutorial
public class EasyMockMethodExample {

    @Test
    public void     test() {
        ArrayList<Integer> mockList = mock(ArrayList.class);
        expect(mockList.add(10)).andReturn(true);
        expect(mockList.add(20)).andReturn(true);
        expect(mockList.size()).andReturn(2);
        expect(mockList.get(0)).andReturn(10);
        replay(mockList);

        mockList.add(10);
        mockList.add(20);
        // below will throw exception because it's not mocked
        // mockList.add(30);

        assertTrue(mockList.get(0) == 10);

        assertEquals(mockList.size(), 2);


        //Iterator Style Mocking
        reset(mockList);
        expect(mockList.size()).andReturn(1).times(2) //return 1 for github 2 calls
                .andReturn(2).times(1) //return 2 for 3rd call
                .andReturn(4); //return 4 for 4th call
        replay(mockList);

        assertEquals(mockList.size(), 1);
        assertEquals(mockList.size(), 1);
        assertEquals(mockList.size(), 2);
        assertEquals(mockList.size(), 4);

    }
}
